﻿package Gameplay.Visualizer
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;

	public class EqualizerBar extends Sprite 
	{   
		public static const NUM_RECT:uint = 33;	   
		public static const LENGTH:uint = 5;	   
		public static const MARGIN:uint = 2;	   
		public static const MAX_LENGTH:uint = (LENGTH + MARGIN) * NUM_RECT - MARGIN;   
		
		private var FRects:Vector.<Bitmap>;
	  	private var FLevel:Number;
		
		public function EqualizerBar(AColor:uint=0xffffff, AGradient:Boolean=false) 
		{
			FLevel = new Number();
			
			var r:uint = (AColor & 0x00ff0000) >> 16;
			var g:uint = (AColor & 0x0000ff00) >> 8;
			var b:uint = (AColor & 0x000000ff);
			
			FRects = new Vector.<Bitmap>;
			for (var i:uint = 0; i < NUM_RECT; i++) 
			{
				var vGradient:Number = Math.min(Math.sqrt((i+1) / NUM_RECT + 0.4), 1);
				var vColor:uint = (r * vGradient << 16) | (g * vGradient << 8) | b * vGradient;
				var vBMP:Bitmap = new Bitmap(new BitmapData(LENGTH, LENGTH, false, AGradient ? vColor : AColor));				
				vBMP.x = i * (LENGTH + MARGIN);
				FRects[i] = vBMP;
				addChild(vBMP);
				vBMP.visible = false;
			}
		}
		
		public function set Level(ALevel:Number):void 
		{
			FLevel = Math.min(NUM_RECT, Math.floor(NUM_RECT * ALevel));			
			for (var i in FRects)
				FRects[i].visible = i < FLevel;
		}
		
		public function get LevelWidth():Number
		{
			return (LENGTH + MARGIN) * FLevel;
		}
	}
}
